﻿using LabManagement.Data;
using OnlineLabManagement.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineLabManagement.Areas.AdminConsole.Controllers
{
    public class OrgController : DbControllerBase
    {
        //
        // GET: /AdminConsole/Org/

        public ActionResult Index(int? page, int? pagesize)
        {
            var PageNumber = (page ?? 1);
            var PageSize = (pagesize ?? 5);
            var orgs = this.DatabaseFactory.OrgUtils.Orgs();
            return View(orgs);            
        }

        //
        // GET: /AdminConsole/Org/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AdminConsole/Org/Create

        public ActionResult Create()
        {
            return View();
        }

       
        public ActionResult AddPhone(int id)
        {
            ViewBag.No = id;
            return View(new Phone());
        }
        public ActionResult AddAddress(int id)
        {
            ViewBag.No = id;
            return View(new Address());
        }
        //
        // POST: /AdminConsole/Org/Create

        [HttpPost]
        public ActionResult Create(Org org)
        {
            try
            {
                org.OrgId = Guid.NewGuid();
                org.Contact.ContactId=Guid.NewGuid();
                foreach (var contactAddresses in org.Contact.ContactAddresses)
                {
                    contactAddresses.ContactAddressId = Guid.NewGuid(); ;
                    contactAddresses.Address.AddressId = Guid.NewGuid();                   
                }
                foreach (var contactPhones in org.Contact.ContactPhones)
                {
                    contactPhones.ContactPhoneId = Guid.NewGuid();
                    contactPhones.Phone.PhoneId = Guid.NewGuid();
                }
                this.DataBridge.Entry(org).State = System.Data.Entity.EntityState.Added;
                this.DataBridge.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }



        
        //
        // GET: /AdminConsole/Org/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /AdminConsole/Org/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminConsole/Org/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AdminConsole/Org/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
